package com.zscat.mallplus.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zscat.mallplus.annotation.SysLog;
import com.zscat.mallplus.cms.entity.CmsHelp;
import com.zscat.mallplus.sys.entity.SysStoreSettleRecords;
import com.zscat.mallplus.sys.service.ISysStoreSettleRecordsService;
import com.zscat.mallplus.util.EasyPoiUtils;
import com.zscat.mallplus.utils.CommonResult;
import com.zscat.mallplus.utils.ValidatorUtils;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Date;

/**
 * @author mallplus
 * @date 2020-06-26
 * 商户结算
 */
@Slf4j
@RestController
@RequestMapping("/sys/sysStoreSettleRecords")
public class SysStoreSettleRecordsController {

    @Resource
    private ISysStoreSettleRecordsService ISysStoreSettleRecordsService;

    @SysLog(MODULE = "sys", REMARK = "根据条件查询所有商户结算列表")
    @ApiOperation("根据条件查询所有商户结算列表")
    @GetMapping(value = "/list")
    public Object getSysStoreSettleRecordsByPage(SysStoreSettleRecords entity,
                                                 @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                 @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
    ) {
        try {
            return new CommonResult().success(ISysStoreSettleRecordsService.page(new Page<SysStoreSettleRecords>(pageNum, pageSize), new QueryWrapper<>(entity)));
        } catch (Exception e) {
            log.error("根据条件查询所有商户结算列表：%s", e.getMessage(), e);
        }
        return new CommonResult().failed();
    }

    @SysLog(MODULE = "sys", REMARK = "保存商户结算")
    @ApiOperation("保存商户结算")
    @PostMapping(value = "/create")
    @PreAuthorize("hasAuthority('sys:sysStoreSettleRecords:create')")
    public Object saveSysStoreSettleRecords(@RequestBody SysStoreSettleRecords entity) {
        try {
            entity.setCreateTime(new Date());
            if (ISysStoreSettleRecordsService.save(entity)) {
                return new CommonResult().success();
            }
        } catch (Exception e) {
            log.error("保存商户结算：%s", e.getMessage(), e);
            return new CommonResult().failed(e.getMessage());
        }
        return new CommonResult().failed();
    }

    @SysLog(MODULE = "sys", REMARK = "更新商户结算")
    @ApiOperation("更新商户结算")
    @PostMapping(value = "/update/{id}")
    @PreAuthorize("hasAuthority('sys:sysStoreSettleRecords:update')")
    public Object updateSysStoreSettleRecords(@RequestBody SysStoreSettleRecords entity) {
        try {
            if (ISysStoreSettleRecordsService.updateById(entity)) {
                return new CommonResult().success();
            }
        } catch (Exception e) {
            log.error("更新商户结算：%s", e.getMessage(), e);
            return new CommonResult().failed(e.getMessage());
        }
        return new CommonResult().failed();
    }

    @SysLog(MODULE = "sys", REMARK = "删除商户结算")
    @ApiOperation("删除商户结算")
    @GetMapping(value = "/delete/{id}")
    @PreAuthorize("hasAuthority('sys:sysStoreSettleRecords:delete')")
    public Object deleteSysStoreSettleRecords(@ApiParam("商户结算id") @PathVariable Long id) {
        try {
            if (ValidatorUtils.empty(id)) {
                return new CommonResult().paramFailed("商户结算id");
            }
            if (ISysStoreSettleRecordsService.removeById(id)) {
                return new CommonResult().success();
            }
        } catch (Exception e) {
            log.error("删除商户结算：%s", e.getMessage(), e);
            return new CommonResult().failed(e.getMessage());
        }
        return new CommonResult().failed();
    }

    @SysLog(MODULE = "sys", REMARK = "给商户结算分配商户结算")
    @ApiOperation("查询商户结算明细")
    @GetMapping(value = "/{id}")
    @PreAuthorize("hasAuthority('sys:sysStoreSettleRecords:read')")
    public Object getSysStoreSettleRecordsById(@ApiParam("商户结算id") @PathVariable Long id) {
        try {
            if (ValidatorUtils.empty(id)) {
                return new CommonResult().paramFailed("商户结算id");
            }
            SysStoreSettleRecords coupon = ISysStoreSettleRecordsService.getById(id);
            return new CommonResult().success(coupon);
        } catch (Exception e) {
            log.error("查询商户结算明细：%s", e.getMessage(), e);
            return new CommonResult().failed();
        }

    }

    @ApiOperation(value = "批量删除商户结算")
    @RequestMapping(value = "/delete/batch", method = RequestMethod.GET)
    @SysLog(MODULE = "sys", REMARK = "批量删除商户结算")
    @PreAuthorize("hasAuthority('sys:sysStoreSettleRecords:delete')")
    public Object deleteBatch(@RequestParam("ids") List
            <Long> ids) {
        boolean count = ISysStoreSettleRecordsService.removeByIds(ids);
        if (count) {
            return new CommonResult().success(count);
        } else {
            return new CommonResult().failed();
        }
    }


    @SysLog(MODULE = "sys", REMARK = "导出社区数据")
    @GetMapping("/exportExcel")
    public void export(HttpServletResponse response, SysStoreSettleRecords entity) {
        // 模拟从数据库获取需要导出的数据
        List<SysStoreSettleRecords> personList = ISysStoreSettleRecordsService.list(new QueryWrapper<>(entity));
        // 导出操作
        EasyPoiUtils.exportExcel(personList, "导出社区数据", "社区数据", SysStoreSettleRecords.class, "导出社区数据.xls", response);

    }

    @SysLog(MODULE = "sys", REMARK = "导入社区数据")
    @PostMapping("/importExcel")
    public void importUsers(@RequestParam MultipartFile file) {
        List<SysStoreSettleRecords> personList = EasyPoiUtils.importExcel(file, SysStoreSettleRecords.class);
        ISysStoreSettleRecordsService.saveBatch(personList);
    }

    @ApiOperation("修改展示状态")
    @RequestMapping(value = "/update/updateShowStatus")
    @ResponseBody
    @SysLog(MODULE = "sys", REMARK = "修改展示状态")
    public Object updateShowStatus(@RequestParam("ids") Long ids,
                                   @RequestParam("showStatus") Integer showStatus) {
        SysStoreSettleRecords record = new SysStoreSettleRecords();
        record.setStatus(showStatus);
        record.setId(ids);
        record.setUpdateTime(new Date());
        if (ISysStoreSettleRecordsService.updateById(record)) {
            return new CommonResult().success();
        } else {
            return new CommonResult().failed();
        }
    }
}


